package com.iman.common.hibernate;


import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.iman.common.support.Page;

public interface HibernateSupport {
	
	//之所以写着3个方法，是应为如果你直接调用hiberanteTemplate的save方法，是不走代理的，也就是事务没开启。这个问题是在save blob的时候发现的。
	public abstract Serializable save(Object entity);
	public abstract void update(Object entity);
	public abstract void saveOrUpdate(Object entity);
	public abstract void merge(Object entity);
	public abstract void saveOrUpdateAll(Collection collections);
	public void delete(Object entity);
	
	public abstract void saveAll(Collection entities);

	public abstract void updateAll(Collection entities);

	public abstract List findBySQLQuery(String sql);

	public abstract List findBySQLQuery(String sql, Map params);

	public abstract Page findByPagedSQLQuery(String sql, Map params);

	public abstract Page pagedQuery(Class entityClass, int pageNo, int pageSize,
			String orderBy, boolean isAsc, Criterion... criterions);

	public abstract Page pagedQuery(Class entityClass, int pageNo, int pageSize,
			Criterion... criterions);

	public abstract Page pagedQuery(Criteria criteria, int pageNo, int pageSize,
			String distinctProperty);

	public abstract Page pagedQuery(Criteria criteria, int pageNo, int pageSize);

	public abstract List findByNamedSQLQuery(String queryName, Map params);

	public abstract Page findByNamedPagedSQLQuery(String queryName, Map params);

	public abstract Page pagedByHQL(String hql, String startS, String limitS,
			Object... values);

	public abstract Page pagedByHQLName(String hqlName, String startS,
			String limitS, Object... values);

	public abstract Page pagedLimitQuery(String queryName, String startS,
			String limitS, Map params);

	public abstract <T> T findUniqueBy(Class<T> entityClass, String propertyName, Object value);

	public abstract <T> List<T> findBy(Class<T> entityClass, String propertyName, Object value,
			String orderBy, boolean isAsc);

	public abstract <T> List<T> findBy(Class<T> entityClass, String propertyName, Object value);

	public abstract <T> Criteria createCriteria(Class<T> entityClass, Criterion... criterions);

	public abstract <T> Criteria createCriteria(Class<T> entityClass, String orderBy,
			boolean isAsc, Criterion... criterions);

	public abstract Query createQuery(String hql, Object... values);

	public abstract Object findEntityAssociate(Class clazz, Serializable id,
			String[] properties);
	
	public abstract <T> List<T> findBySQLQuery(String sql, Map params, Class<T> entityClass);
	public abstract <T> Page findByPagedSQLQuery(String sql, Map params,Class<T> entityClass);
	
	//主要是为删除用
	public abstract void bulkUpdate(final String queryString, final Object... values) ;
	
	//sql 更新
	public int updateNamedSQLQuery(String queryName, Map params);
	
	//wenzhi.zhang 20110705 added 
    public abstract List findByNamedParam(String queryString, Map params);
    
    //=======================copy from HibernateSupport===========================//
    List find(String queryString) throws DataAccessException;

    /**
     * Execute an HQL query, binding one value to a "?" parameter in the
     * query string.
     * @param queryString a query expressed in Hibernate's query language
     * @param value the value of the parameter
     * @return a {@link List} containing the results of the query execution
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.Session#createQuery
     */
    List find(String queryString, Object value) throws DataAccessException;

    /**
     * Execute an HQL query, binding a number of values to "?" parameters
     * in the query string.
     * @param queryString a query expressed in Hibernate's query language
     * @param values the values of the parameters
     * @return a {@link List} containing the results of the query execution
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.Session#createQuery
     */
    List find(String queryString, Object... values) throws DataAccessException;
    
    List findByValueBean(String queryString, Object valueBean) throws DataAccessException;
    List findByCriteria(DetachedCriteria criteria) throws DataAccessException;
    
    List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException;

    /**
     * Execute a query based on the given example entity object.
     * @param exampleEntity an instance of the desired entity,
     * serving as example for "query-by-example"
     * @return a {@link List} containing 0 or more persistent instances
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.criterion.Example#create(Object)
     */
    List findByExample(Object exampleEntity) throws DataAccessException;

    /**
     * Execute a query based on the given example entity object.
     * @param entityName the name of the persistent entity
     * @param exampleEntity an instance of the desired entity,
     * serving as example for "query-by-example"
     * @return a {@link List} containing 0 or more persistent instances
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.criterion.Example#create(Object)
     */
    List findByExample(String entityName, Object exampleEntity) throws DataAccessException;

    /**
     * Execute a query based on a given example entity object.
     * @param exampleEntity an instance of the desired entity,
     * serving as example for "query-by-example"
     * @param firstResult the index of the first result object to be retrieved
     * (numbered from 0)
     * @param maxResults the maximum number of result objects to retrieve
     * (or <=0 for no limit)
     * @return a {@link List} containing 0 or more persistent instances
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.criterion.Example#create(Object)
     * @see org.hibernate.Criteria#setFirstResult(int)
     * @see org.hibernate.Criteria#setMaxResults(int)
     */
    List findByExample(Object exampleEntity, int firstResult, int maxResults) throws DataAccessException;

    /**
     * Execute a query based on a given example entity object.
     * @param entityName the name of the persistent entity
     * @param exampleEntity an instance of the desired entity,
     * serving as example for "query-by-example"
     * @param firstResult the index of the first result object to be retrieved
     * (numbered from 0)
     * @param maxResults the maximum number of result objects to retrieve
     * (or <=0 for no limit)
     * @return a {@link List} containing 0 or more persistent instances
     * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
     * @see org.hibernate.criterion.Example#create(Object)
     * @see org.hibernate.Criteria#setFirstResult(int)
     * @see org.hibernate.Criteria#setMaxResults(int)
     */
    List findByExample(String entityName, Object exampleEntity, int firstResult, int maxResults) throws DataAccessException;
    
    <T> T get(Class<T> entityClass, Serializable id) throws DataAccessException;
    <T> T load(Class<T> entityClass, Serializable id) throws DataAccessException;
}
